/**
* +--------------------------------------------------------+
* @Created by Goland.
* @Category user.go
* @Depiction
* @Author hwy <429208948@qq.com>
* @Date 2021/9/24  14:36
* +--------------------------------------------------------+
 */
package service

import (
	"go_frames/app/dao"
	"go_frames/app/model"
)

// 中间件管理服务
var User = new(userService)

type userService struct{}

func (s *userService) GetList(req *model.UserPageReq) ([]model.UserInfoVo, int, error) {
	// 创建查询实例
	query := dao.User.Where("mark=1")

	// 查询条件
	if req != nil {
		// 用户姓名
		if req.Realname != "" {
			query = query.Where("realname like ?", "%"+req.Realname+"%")
		}
		// 姓名
		if req.Gender > 0 {
			query = query.Where("gender=?", req.Gender)
		}
	}

	// 查询记录
	count, err := query.Count()
	if err != nil {
		return nil, 0, err
	}
	// 排序
	query = query.Order("sort asc")
	// 分页
	data := query.Page(req.Page, req.Limit)
	// 对象转化
	var list []model.User
	data.Structs(&list)

	// 获取职级列表
	levelList, _ := dao.Level.Where("mark=1").Fields("id,name").All()
	//var levelLists []model.Level
	//levelList.Structs(&levelLists)

	var levelMap = make(map[int]string)
	for _, v := range levelList {
		//levelMap[v.Id] = v.Name
		levelMap[v["id"].Int()] = v["name"].String()
	}
	// 数据处理
	var result []model.UserInfoVo
	for _, v := range list {
		item := model.UserInfoVo{}
		item.User = v

		// 职级
		if v.LevelId > 0 {
			item.LevelName = levelMap[v.LevelId]
		}

		//加入数组
		result = append(result, item)
	}

	return result, count, nil


}
